﻿Imports System.Data
Imports System.Data.SqlClient
Imports System.ComponentModel
Public Class LopDAO
    Public Sub LoadCombobox(ByVal cbx As ComboBox, ByVal sql As String)
        Dim load As DataProvider = New DataProvider
        cbx.DataSource = load.Sql_ExecuteTable(sql)
    End Sub
    Public Sub LoadGridView(ByVal dgv As DataGridView, ByVal sql As String)
        Dim load As DataProvider = New DataProvider
        dgv.DataSource = load.Sql_ExecuteTable(sql)
    End Sub
    Public Function ThemLop(ByVal lopInsert As LopDTO) As Boolean
        Dim Insert As DataProvider = New DataProvider
        Dim sql As String = String.Format("Insert_Lop")
        Dim pr As SqlParameter() = {New SqlParameter("@MaLop", lopInsert.MaLop),
                                    New SqlParameter("@TenLop", lopInsert.TenLop),
                                    New SqlParameter("@SiSo", lopInsert.SiSo),
                                    New SqlParameter("@NgayLap", lopInsert.NgayLap),
                                    New SqlParameter("@MaKhoi", lopInsert.MaKhoi)}
        Dim cmd As SqlCommand = New SqlCommand(sql)
        cmd.CommandType = CommandType.StoredProcedure
        For i As Integer = 0 To pr.Count() - 1
            cmd.Parameters.Add(pr(i))
        Next
        Return Insert.SQL_ExecuteNonQuery(cmd)
    End Function
    Public Function XoaLop(ByVal id As String) As Boolean
        Dim Remove As DataProvider = New DataProvider
        Dim sql As String = String.Format("Remove_Lop")
        Dim pr As SqlParameter = New SqlParameter("@MaLop", id)
        Dim cmd As SqlCommand = New SqlCommand(sql)
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Parameters.Add(pr)
        Return Remove.SQL_ExecuteNonQuery(cmd)
    End Function
    Public Function SuaLop(ByVal lopUpdate As LopDTO) As Boolean
        Dim Update As DataProvider = New DataProvider
        Dim sql As String = String.Format("Update_LopHoc")
        Dim pr As SqlParameter() = {New SqlParameter("@MaLop", lopUpdate.MaLop),
                                    New SqlParameter("@TenLop", lopUpdate.TenLop),
                                    New SqlParameter("@SiSo", lopUpdate.SiSo),
                                    New SqlParameter("@NgayLap", lopUpdate.NgayLap),
                                    New SqlParameter("@MaKhoi", lopUpdate.MaKhoi)}
        Dim cmd As SqlCommand = New SqlCommand(sql)
        cmd.CommandType = CommandType.StoredProcedure
        For i As Integer = 0 To pr.Count() - 1
            cmd.Parameters.Add(pr(i))
        Next
        Return Update.SQL_ExecuteNonQuery(cmd)
    End Function

    Public Function GetList() As BindingList(Of LopDTO)
        Dim _ListLop As New BindingList(Of LopDTO)
        Dim GetDT As DataProvider = New DataProvider
        Dim sql As String = "select MaLop, TenLop, SiSo, NgayLap, MaKhoi from LOP"
        Dim rd As SqlDataReader
        rd = GetDT.SQL_ExcuteQueryReader(sql)
        While rd.Read()
            Dim item As New LopDTO()
            item.MaLop = rd.GetString(0)
            item.TenLop = rd.GetString(1)
            item.SiSo = rd.GetInt32(2)
            item.NgayLap = rd.GetDateTime(3)
            item.MaKhoi = rd.GetString(4)
            _ListLop.Add(item)
        End While
        Return _ListLop
    End Function
End Class
